package com.excilys.training.ebanking.web.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.excilys.training.ebanking.iservice.IServiceCompte;
import org.excilys.training.ebanking.iservice.IServiceOperation;
import org.excilys.training.ebanking.model.Compte;
import org.excilys.training.ebanking.model.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.excilys.training.ebanking.web.security.UtilisateurPersonnalise;

@Controller
@RequestMapping("/client/virement")
public class VirementController {
	@Autowired
	IServiceCompte serviceCompte;

	@Autowired
	IServiceOperation serviceOperation;

	@RequestMapping("/interne.html")
	public ModelAndView redirectVirement(ModelMap model) {
		UtilisateurPersonnalise utilisateur = (UtilisateurPersonnalise) SecurityContextHolder
				.getContext().getAuthentication().getPrincipal();
		model.addAttribute("comptes",
				serviceCompte.getAllAccountById(utilisateur.getId()));
		return new ModelAndView("/client/virement_interne");
	}

	@RequestMapping("/externe.html")
	public ModelAndView redirectExterne(ModelMap model) {
		UtilisateurPersonnalise utilisateur = (UtilisateurPersonnalise) SecurityContextHolder
				.getContext().getAuthentication().getPrincipal();
		model.addAttribute("comptes",
				serviceCompte.getAllAccountById(utilisateur.getId()));
		return new ModelAndView("/client/virement_externe");
	}

	@RequestMapping("/validation.html")
	public String redirectVirementValidation(HttpServletRequest request) {
		int compteSource = Integer.parseInt(request
				.getParameter("compteSource"));
		int operationType = Integer.parseInt(request
				.getParameter("operationType"));
		int CompteCible = Integer.parseInt(request.getParameter("compteCible"));
		double montant = Double.parseDouble(request.getParameter("montant"));

		return new String("redirect:/client/virement/validation/"
				+ operationType + "/" + compteSource + "/" + CompteCible + "/"
				+ montant + ".html");
	}

	@RequestMapping("/validation/{operationType}/{compteSource}/{compteCible}/{montant}.html")
	public String redirectVirementValidation2(
			@PathVariable("operationType") int operationType,
			@PathVariable("compteSource") int compteSource,
			@PathVariable("compteCible") int compteCible,
			@PathVariable("montant") double montant, ModelMap model) {
		if (serviceCompte.testSoldeCompte(compteSource, montant) == false) {
			return new String(
					"redirect:/client/virement/validation/refuse.html");
		}
		if (operationType == 4) {
			serviceCompte.transfertInterne(compteSource, compteCible, montant,
					operationType);
		}
		if (operationType == 5) {
			serviceCompte.transfertExterne(compteSource, compteCible, montant,
					operationType);
		}
		return new String("redirect:/client/virement/validation/accepte.html");
	}

	@RequestMapping("/validation/refuse.html")
	public ModelAndView redirectVirementrefuse() {
		return new ModelAndView("/client/virement/refuse");
	}

	@RequestMapping("/validation/accepte.html")
	public ModelAndView redirectVirementaccepte() {
		return new ModelAndView("/client/virement/validation");
	}

	@RequestMapping("/historique.html")
	public ModelAndView redirectVirementHistorique(ModelMap model) {
		UtilisateurPersonnalise utilisateur = (UtilisateurPersonnalise) SecurityContextHolder
				.getContext().getAuthentication().getPrincipal();
		List<Compte> listCompte = serviceCompte.getAllAccountById(utilisateur
				.getId());
		model.addAttribute("listCompte", listCompte);
		return new ModelAndView("/client/historique_virement");
	}

	@RequestMapping("/historique/{compte}.html")
	public ModelAndView redirectVirementHistorique(ModelMap model,
			@PathVariable("compte") int idCompte) {
		UtilisateurPersonnalise utilisateur = (UtilisateurPersonnalise) SecurityContextHolder
				.getContext().getAuthentication().getPrincipal();
		List<Compte> listCompte = serviceCompte.getAllAccountById(utilisateur
				.getId());
		Compte compte = serviceCompte.getAccount(idCompte);
		List<Operation> listVirement = serviceOperation
				.getAllVirementByAccount(idCompte);
		model.addAttribute("compte", compte);
		model.addAttribute("listVirement", listVirement);
		model.addAttribute("listCompte", listCompte);
		return new ModelAndView("/client/historique_virement");
	}

	@RequestMapping("/historique/validation.html")
	public String redirectFormVirement(HttpServletRequest request) {
		String compteStr = request.getParameter("moisChoisi");
		int compte = Integer.parseInt(compteStr);

		return "redirect:/client/virement/historique/" + compte + ".html";
	}
}
